package copyengine.resource.lazyload
{
	import copyengine.resource.dto.LazyLoadInitDto;
	import copyengine.ui.CESprite;
	import copyengine.utils.GeneralUtils;
	import copyengine.utils.ResUtils;
	import copyengine.utils.cache.object.CECacheDisplayObject;
	import copyengine.utils.cache.object.ICEMovieClip;
	import copyengine.utils.debug.CELog;

	public class CELazyLoadCacheContainer extends CESprite implements ICELazyLoadContainer, ICEMovieClip
	{
		private static const TYPE_DO_NOTHING:int = 0;
		private static const TYPE_GOTO_AND_STOP:int = 1;
		private static const TYPE_GOTO_AND_PLAY:int = 2;

		private var fileName:String;
		private var className:String;
		private var cacheType:int;

		private var childMc:CECacheDisplayObject;

		private var childInitType:int = TYPE_DO_NOTHING;
		private var childStopFrame:int = -1;

		/**
		 * 默认情况不进行水平翻转和垂直翻转
		 */
		private var horizontalFlip:int = 1;
		private var verticalFlip:int = 1;

		private var isPauseMc:Boolean = false;
		private var isPlayRandomFrame:Boolean = false;
		private var isVisable:Boolean = true;

		private var playFinishedCallBackFunctionVector:Vector.<Function>;
		private var loadComplateCallBackFunctionVector:Vector.<Function>;


		public function CELazyLoadCacheContainer()
		{
		}

		//============================================//
		//==Initialize&Dispose  Function
		//============================================//
		public function initialize(_dto:LazyLoadInitDto):void
		{
			fileName = _dto.fileName;
			className = _dto.className;
			cacheType = _dto.cacheType;
			LazyLoadContainerInitHelper.addLazyLoadIcon(_dto, this);
		}

		//============================================//
		//==Interface  ICELazyLoadContainer Function
		//============================================//
		public function notificationLazyLoaderTargetLoadComplate():void
		{
			LazyLoadContainerInitHelper.removeLazyLoadIcon(this);
			childMc = ResUtils.getCacheDisplayObject(className, fileName, cacheType) as CECacheDisplayObject;
			addChild(childMc);
			switch (childInitType)
			{
				case TYPE_DO_NOTHING:
					break;
				case TYPE_GOTO_AND_PLAY:
					childMc.gotoAndPlay(childStopFrame);
					break;
				case TYPE_GOTO_AND_STOP:
					childMc.gotoAndStop(childStopFrame);
					break;
			}
			childMc.setHorizontalFlip(horizontalFlip);
			childMc.setVerticalFlip(verticalFlip);

			isPauseMc && childMc.mcPause();
			isPlayRandomFrame && childMc.gotoAndPlayRandFrame();
			childMc.visible = isVisable;

			if (loadComplateCallBackFunctionVector != null)
			{
				while (loadComplateCallBackFunctionVector.length > 0)
				{
					var f:Function = loadComplateCallBackFunctionVector.pop();
					f && f();
				}
			}

			if (playFinishedCallBackFunctionVector != null)
			{
				while (playFinishedCallBackFunctionVector.length > 0)
				{
					childMc.addMovieClipPlayFinishedCallBackFunction(playFinishedCallBackFunctionVector.pop());
				}
			}
		}

		public function addLoadComplateCallBackFunction(_f:Function):void
		{
			if (childMc == null)
			{
				if (loadComplateCallBackFunctionVector == null)
				{
					loadComplateCallBackFunctionVector = new Vector.<Function>();
				}
				loadComplateCallBackFunctionVector.push(_f);
			}
			else
			{
				_f();
			}
		}

		public function removeLoadComplateCallBack(_f:Function):void
		{
			if (loadComplateCallBackFunctionVector != null)
			{
				for (var i:int = 0; i < loadComplateCallBackFunctionVector.length; i++)
				{
					if (loadComplateCallBackFunctionVector[i] == _f)
					{
						loadComplateCallBackFunctionVector.splice(i, 1);
						break;
					}
				}
			}
		}

		//============================================//
		//==Interface  ICEMovieClip Function
		//============================================//

		public function get totalFrames():int
		{
			if (childMc)
			{
				return childMc.totalFrames;
			}
			else
			{
				CELog.err("CELazyLoadCacheContainer->getTotalFrame: CELazyLoadCacheContainer not loadComplate");
				return 1;
			}
		}

		public function addMovieClipPlayFinishedCallBackFunction(_f:Function):void
		{
			if (childMc)
			{
				childMc.addMovieClipPlayFinishedCallBackFunction(_f);
			}
			else
			{
				if (playFinishedCallBackFunctionVector == null)
				{
					playFinishedCallBackFunctionVector = new Vector.<Function>();
				}
				for each (var callBackFunction:Function in playFinishedCallBackFunctionVector)
				{
					if (callBackFunction == _f)
					{
						return;
					}
				}
				playFinishedCallBackFunctionVector.push(_f);
			}
		}


		public function gotoAndStop(_frame:int):void
		{
			if (childMc == null)
			{
				childStopFrame = _frame;
				childInitType = TYPE_GOTO_AND_STOP;
			}
			else
			{
				childMc.gotoAndStop(_frame);
			}
		}

		public function gotoAndPlay(_frame:int):void
		{
			if (childMc == null)
			{
				childStopFrame = _frame;
				childInitType = TYPE_GOTO_AND_PLAY;
			}
			else
			{
				childMc.gotoAndPlay(_frame);
			}
		}

		public function gotoAndPlayRandFrame():void
		{
			childMc != null ? childMc.gotoAndPlayRandFrame() : isPlayRandomFrame = true;
		}

		public function mcPause():void
		{
			childMc != null ? childMc.mcPause() : isPauseMc = true;
		}

		public function mcContinue():void
		{
			childMc != null ? childMc.mcContinue() : isPauseMc = false;
		}

		public function setHorizontalFlip(_dir:int):void
		{
			horizontalFlip = _dir;
			childMc && childMc.setHorizontalFlip(horizontalFlip);
		}

		public function setVerticalFlip(_dir:int):void
		{
			verticalFlip = _dir;
			childMc && childMc.setVerticalFlip(_dir);
		}

		override protected function doDispose():void
		{
			GeneralUtils.removeTargetFromParent(childMc);
		}

		override public function get visible():Boolean
		{
			return isVisable;
		}

		override public function set visible(value:Boolean):void
		{
			isVisable = value;
			super.visible = value;
			childMc != null ? childMc.visible = value : null;
		}

	}
}
